package com.tjsinfo.tjsoa.Dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;

import com.tjsinfo.tjsoa.common.PagenatedSql;
import com.tjsinfo.tjsoa.common.PaginationSupport;



public class RQSOABspringDao extends SimpleJdbcDaoSupport implements ISpringDao {
	
	
	
	public List<Map<String,String>> commonSelect(String[] columns,String sql){
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		list = new CommonQuery(getDataSource(),sql,columns).execute();
		return list;
	}
	class CommonQuery extends MappingSqlQuery {
		String[] columns;
		CommonQuery(DataSource ds,String sql,String[] columns) {
			super(ds, sql);
			this.columns=columns;
			compile();
		}
		protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Map<String,String> map=new HashMap<String, String>();
			map= getRow(rs,map,columns);
			return map;
		}
	}
	private Map<String,String> getRow(ResultSet rs,Map<String,String> map,String[] columns) throws SQLException{
		for (String column : columns) {
			map.put(column, rs.getString(column));
		}
		return map;
	}
	
	public PaginationSupport buildSQLResultSet(String sql,String columns[],int pageSize,int startIndex,int totalCount){
		List<Map<String, String>> items=new ArrayList<Map<String, String>>();
		if(totalCount==0){//����������
			String counts[]={"counts"};
			String selectCountSQL=PagenatedSql.buildCountSQL(sql, counts[0]);
			List<Map<String, String>> countResult=commonSelect(counts, selectCountSQL);
			for (Map<String, String> map2 : countResult) {
				totalCount=Integer.valueOf(map2.get("counts"));
			}
		}
		sql=PagenatedSql.buildSQLResultSet(sql, pageSize,startIndex);
		items=commonSelect(columns, sql);
		

        PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
		return ps;
	}
	
	public PaginationSupport buildSQLResultSet(String sql,String columns[],String id,int pageSize,int startIndex,int totalCount){
		
		List<Map<String, String>> items=new ArrayList<Map<String, String>>();
		if(totalCount==0){//����������
			String counts[]={"counts"};
			String selectCountSQL=PagenatedSql.buildCountSQL(sql, counts[0]);
			List<Map<String, String>> countResult=commonSelect(counts, selectCountSQL);
			for (Map<String, String> map2 : countResult) {
				totalCount=Integer.valueOf(map2.get("counts"));
			}
		}
		sql=PagenatedSql.buildSQLResultSet(sql,id, pageSize,startIndex);
		items=commonSelect(columns, sql);
		

        PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
		return ps;
	}
	public void commonUpdate(final String sql,Object obj[]) {	
		getJdbcTemplate().update(sql,obj);
	} 
	
	
	public List selectSQLQuery(String sql,ParameterizedRowMapper parameterizedRowMapper,Object[] paramerterArray){
		return  getSimpleJdbcTemplate().query(sql, parameterizedRowMapper, paramerterArray);
	}
	
	
	public Boolean runningSql(String sql,PreparedStatementSetter pss){
		getJdbcTemplate().update(sql, pss);
		return true;
	}

	public Boolean runningSql(String sql){
		getJdbcTemplate().execute(sql);
		return true;
	} 
	
	
	public List querySQL(String sql,Object[] objects,RowMapper rowMapper){
	  return 	getJdbcTemplate().query(sql, objects, rowMapper);
		
	}
}


